<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* ****************************************************************************
  NAIYUMIE KING WANG JJANG Front End Developer <naiyumie@gmail.com>
  FileType:CRLF, Encording:UTF-8, Tab&Space:4/4
  ------------------------------------------------------------------------
  샘플 개발
**************************************************************************** */

class ADeveloperSampleM extends N_Model {

	/***************************************************
	 * preprocess
	 *     constructor
	 *     function
	 *     struct and global values
	 ***************************************************/
	var $tableName;
	function __construct() {
		parent::__construct();
		$this->tableName = 'tbl_a_developer_sample';
	}


	/***************************************************
	 * c
	 ***************************************************/
	function c01($argArray = array()){
		$sqlSetStatment = '';
		$sqlSetStatment = $this->getQuerySetStateMent($argArray);
		$sql = sprintf('
			INSERT INTO
				tbl_a_developer_sample
			SET
				%1$s;',
			$sqlSetStatment
		);
		$this->db->trans_start();
		$this->db->query($sql, $argArray);
		$this->db->trans_complete();
		if ($this->db->trans_status() === TRUE) {
			$result = TRUE;
		}
		if($result){
			$retVal = TRUE;
		} else {
			$retVal = FALSE;
		}
		return $retVal;
	}


	/***************************************************
	 * r
	 ***************************************************/
	function r01($argArray = array()){
		# 상세
		$whereStatement01 = '';
		if(isset($argArray['pk_uid'])===TRUE){
			$whereStatement01 = sprintf('
				 WHERE pk_uid = %s',
				 $this->db->escape_str($argArray['pk_uid'])
			);
		}

		# 검색
		$whereStatement02 = '';
		$whereKey = '';
		$whereVal = '';
		if(isset($argArray['where'])===TRUE){
			$whereKey = $argArray['where']['whereKey'];
			$whereVal = $argArray['where']['whereVal'];
			if($argArray['where']['whereOpt'] == 'absolute'){
				$whereStatement02 = sprintf('
					WHERE %s = "%s" ',
					$this->db->escape_str($whereKey) , $this->db->escape_str($whereVal)
				);
			} else {
				$whereStatement02 = sprintf('
					WHERE %s LIKE "%%%s%%" ',
					$this->db->escape_str($whereKey) , $this->db->escape_str($whereVal)
				);
			}
		}
		//nlog($whereStatement02);

		# 페이지네이션
		$limitStatement = '';
		if(isset($argArray['paginationCfg']['paginationFirst']) === TRUE && isset($argArray['paginationCfg']['paginationSecond']) === TRUE){
			$limitStatement = sprintf('
				 LIMIT %s, %s',
				 $argArray['paginationCfg']['paginationFirst'], $argArray['paginationCfg']['paginationSecond']
			);
		}
		$orderByStatement = ' ORDER BY pk_uid DESC ';

		$sql = sprintf('
			SELECT
				*
			FROM tbl_a_developer_sample
			%1$s
			%2$s
			%3$s;',
			$whereStatement01,
			$orderByStatement,
			$limitStatement
		);
		//nlog('q>>'.$sql);

		$query = $this->db->query($sql);
		$result = $query->result_array($query);
		if(isset($argArray['pk_uid'])===TRUE){
			$retVal = @$result[0];
		} else {
			$retVal = $result;
		}
		return $retVal;
	}

	function r02(){
		$sql = sprintf('
			SELECT
				COUNT(pk_uid) AS cnt
			FROM tbl_a_developer_sample;'
		);
		$query = $this->db->query($sql);
		$result = $query->result_array($query);
		$retVal = @$result[0]['cnt'];
		return $retVal;
	}


	/***************************************************
	 * u
	 ***************************************************/
	function u01($argArray = array()){
		$sqlSetStatment = '';
		$sqlSetStatment = $this->getQuerySetStateMent($argArray);
		$sql = sprintf('
			UPDATE
				tbl_a_developer_sample
			SET
				%1$s
			WHERE pk_uid = %2$s ;',
			$sqlSetStatment,
			$this->db->escape_str($argArray['pk_uid'])
		);
		$this->db->trans_start();
		$this->db->query($sql, $argArray);
		$this->db->trans_complete();
		if ($this->db->trans_status() === TRUE) {
			$result = TRUE;
		}
		if($result){
			$retVal = TRUE;
		} else {
			$retVal = FALSE;
		}
		return $retVal;
	}


	/***************************************************
	 * d
	 ***************************************************/
	function d01($argArray = array()){
		if(empty($argArray['pk_uid']) !== FALSE){
			return FALSE;
		}
		$sql = sprintf('
			DELETE
			FROM
				tbl_a_developer_sample
			WHERE pk_uid = %1$s;',
			$this->db->escape_str(@$argArray['pk_uid'])
		);
		$query = $this->db->query($sql);
		$result = $this->db->affected_rows();
		if($result > 0){
			$retVal = TRUE;
		} else {
			$retVal = FALSE;
		}
		return $retVal;
	}


	/***************************************************
	 * model and value objects
	 ***************************************************/
	function m01(){
		$sql = sprintf('
			SHOW FULL COLUMNS FROM tbl_a_developer_sample'
		);
		$query = $this->db->query($sql);
		$result = $query->result_array($query);
		$retVal = $result;
		return $retVal;
	}
}
/* End of file */